package com.dq.health.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dq.health.dto.request.common.BaseIdRequest;
import com.dq.health.dto.request.notice.QueryNoticeParam;
import com.dq.health.dto.request.notice.SaveNoticeRequest;
import com.dq.health.entity.notice.Notice;
import com.dq.health.enums.NoticeStatusEnum;
import com.dq.health.service.IAPINoticeService;
import com.dq.health.utils.SecurityUtils;
import com.dq.health.utils.data.JsonUtil;
import com.dq.health.utils.view.SimpleView;
import com.dq.health.utils.view.StringView;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <>
 *
 * @Author: chenxin
 * @Date: 2021/5/20
 */
@RequestMapping("/notice")
@RestController
@Api(tags = "公告接口")
public class NoticeController {

    private static final Logger LOGGER = LoggerFactory.getLogger(NoticeController.class);

    @Autowired
    private IAPINoticeService noticeService;

    @PostMapping("/queryNoticeInfoList")
    @ApiOperation("查询公告列表")
    public SimpleView<IPage<Notice>> queryNoticeInfoList(@RequestBody QueryNoticeParam request) {
        LOGGER.info("### queryNoticeInfoList req:{}", JsonUtil.toString(request));
        SimpleView<IPage<Notice>> view = new SimpleView<>();

        IPage<Notice> page = noticeService.queryPageList(request);
        view.success(page);

        return view;
    }

    @PostMapping("/addNotice")
    @ApiOperation("新增公告")
    public SimpleView<Long> addNotice(@RequestBody SaveNoticeRequest request) {
        LOGGER.info("### addNotice req:{}", JsonUtil.toString(request));
        SimpleView<Long> view = new SimpleView<>();

        Long loginUserId = SecurityUtils.getLoginUserId();
        request.setOperationUserId(loginUserId);
        Long noticeId = noticeService.addNotice(request);
        view.success(noticeId);

        return view;
    }

    @PostMapping("/updateNotice")
    @ApiOperation("编辑公告")
    public StringView updateNotice(@RequestBody SaveNoticeRequest request) {
        LOGGER.info("### updateNotice req:{}", JsonUtil.toString(request));
        StringView view = new StringView();

        Long loginUserId = SecurityUtils.getLoginUserId();
        request.setOperationUserId(loginUserId);
        noticeService.updateNotice(request);
        view.success("修改成功");

        return view;
    }

    @PostMapping("/pushNotice")
    @ApiOperation("发布公告")
    public StringView pushNotice(@RequestBody BaseIdRequest request) {
        LOGGER.info("### pushNotice req:{}", JsonUtil.toString(request));
        StringView view = new StringView();

        SaveNoticeRequest noticeRequest = new SaveNoticeRequest();
        noticeRequest.setNoticeId(request.getId());
        Long loginUserId = SecurityUtils.getLoginUserId();
        noticeRequest.setOperationUserId(loginUserId);
        noticeRequest.setNoticeStatus(NoticeStatusEnum.PUSHED.getValue());
        noticeService.updateNotice(noticeRequest);

        view.success("发布成功");
        return view;
    }

    @PostMapping("/shelveNotice")
    @ApiOperation("下架公告")
    public StringView shelveNotice(@RequestBody BaseIdRequest request) {
        LOGGER.info("### shelveNotice req:{}", JsonUtil.toString(request));
        StringView view = new StringView();

        SaveNoticeRequest noticeRequest = new SaveNoticeRequest();
        noticeRequest.setNoticeId(request.getId());
        Long loginUserId = SecurityUtils.getLoginUserId();
        noticeRequest.setOperationUserId(loginUserId);
        noticeRequest.setNoticeStatus(NoticeStatusEnum.WAIT.getValue());
        noticeService.updateNotice(noticeRequest);

        view.success("下架成功");
        return view;
    }

    @PostMapping("/deleteNotice")
    @ApiOperation("删除公告")
    public StringView deleteNotice(@RequestBody BaseIdRequest request) {
        LOGGER.info("### deleteNotice req:{}", JsonUtil.toString(request));
        StringView view = new StringView();

        SaveNoticeRequest noticeRequest = new SaveNoticeRequest();
        noticeRequest.setNoticeId(request.getId());
        Long loginUserId = SecurityUtils.getLoginUserId();
        noticeRequest.setOperationUserId(loginUserId);
        noticeRequest.setNoticeStatus(NoticeStatusEnum.DELETE.getValue());
        noticeService.updateNotice(noticeRequest);

        view.success("删除成功");
        return view;
    }

}
